#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/29 0029 16:50
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : qqcomment

import redis
import pymongo
import chardet
import demjson
from multiprocessing import Process


class Mongo_(object):
    def __init__(self):
        host = 'localhost'
        port = 27018
        db = 'python'
        doc = 'qqcomment'
        con = pymongo.MongoClient(host=host, port=port)
        db = con[db]
        self.db = db
        self.doc = db[doc]

def get_redis(list_name):
    r = redis.Redis(host='localhost', port=6379, db='2')
    return r.rpop(list_name)

def do_insert():
    M = Mongo_()
    while 1:
        value = get_redis("qqlive:comment")
        if len(value) < 5:
            break
        try:
            n_v = demjson.decode(value, encoding="%s" % chardet.detect(value)['encoding'])
            M.doc.insert(n_v)
            print("ok")
        except:
            n_v = demjson.decode(value, encoding='utf-8')
            M.db['com_err'].insert(n_v)
            print("error")

def run():
    worker = list()
    for _ in range(15):
        worker.append(Process(target=do_insert))
    for i in worker:
        i.start()
    for i in worker:
        i.join()
    print("is over")


if __name__ == '__main__':
    run()
